-- UIHint
-- Create by zouyb
-- 提示界面

require "game/ui/debug/UIClientStatus"

-- UIHint继承自Layer
UIHint = class("UIHint", function()
    return cc.Layer:create();
end);

function UIHint.create(title, msg, skipable, autoClose, callback)
    return UIHint.new(title, msg, skipable, autoClose, callback);
end

-- 背景框切片数量
local SMALL_BG_COUNT = 4;

-- 内部函数声明
local resize;

-- 构造函数
function UIHint:ctor(title, msg, skipable, autoClose, callback)
    local node = cc.CSLoader:createNode("layout/dialog/Hint.csb");
    self.node = node;
    self:addChild(node);
    self:setName("UIHint");
    self.autoClose = autoClose;
    self.callback = callback;
    local ct = node:getChildByName("CT");

    -- 标题
    self.titleLabel = ct:getChildByName("title");
    TextStyleM.setTitleStyle(self.titleLabel);
    self.titleLabel:setString(title);

    -- 内容
    self.textLabel = ct:getChildByName("text");
    TextStyleM.setTextStyle(self.textLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    self.textLabel:getVirtualRenderer():setLineHeight(TextStyleM.TEXT_SIZE_SMALL + 5);
    self.textLabel = applyString(self.textLabel, msg);

    -- 客户端日志界面
    local function openClientStatusForm()
        local uiForm = UIClientStatus:create();
        UIMgr.getCurrentScene():addForm(uiForm);
    end

    local bgNode = findChildByName(node, "bg/bgImg");
    bgNode:setTouchEnabled(true);
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            performWithDelay(self, openClientStatusForm, 10);
        elseif eventType == ccui.TouchEventType.ended then
            self:stopAllActions();

            -- 是否可点击跳过
            if not skipable then
                return;
            else
                UIMgr.getCurrentScene():removeDialog(self);
                if callback then
                    callback();
                end
            end
        end
    end
    bgNode:addTouchEventListener(onBgClick);

    local frameNode = findChildByName(node, "CT/image_bg");
    for i = 1, SMALL_BG_COUNT do
        local bgImg = findChildByName(frameNode, "bg"..i);
        bgImg:setTouchEnabled(true);
        bgImg:addTouchEventListener(onBgClick);
    end

    local frameNode = findChildByName(node, "CT/image_bg");
    for i = 1, SMALL_BG_COUNT do
        local bgImg = findChildByName(frameNode, "bg"..i);
        bgImg:setTouchEnabled(true);
        bgImg:addTouchEventListener(onBgClick);
    end

    if autoClose == true then
        local function toClose()
            UIMgr.getCurrentScene():removeDialog(self);
            if self.callback then
                self.callback();
            end
        end
        performWithDelay(self, toClose, 2);
    end

    -- 调整高度
    self:adjustHeight();

    -- 适配
    resize(node);
end

-- 调整高度
function UIHint:adjustHeight()
    -- 老的高度和缩放
    local bgFrame = findChildByName(self.node, "CT/image_bg");
    local oldHeight = 576 * bgFrame:getScaleY();
    local oldScaleY = bgFrame:getScaleY();

    local textHeight = self.textLabel:getContentSize().height;

    local margin = 120;
    local newHeight = textHeight + margin * 2;

    if self.titleLabel:getString() == "" then
        self.textLabel:setPositionY(0);
    end

    if newHeight <= oldHeight then
        return;
    end

    local delta = (newHeight - oldHeight) / 2;

    -- 新的Y轴缩放
    local newScaleY = newHeight * oldScaleY / oldHeight;
    bgFrame:setScaleY(newScaleY);

    -- 标题上移delta像素
    self.titleLabel:setPositionY(self.titleLabel:getPositionY() + delta);

    -- 底图
    local bg1 = findChildByName(self.node, "CT/bg1");
    local bg2 = findChildByName(self.node, "CT/bg2");
    bg1:setPositionY(bg1:getPositionY() + delta);
    bg2:setPositionY(bg2:getPositionY() + delta);
end

-- 适配
function resize(node)
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end

return UIHint;
